<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据编号" prop="billNo">
        <el-input
          v-model="queryParams.billNo"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编号" prop="productNo">
        <el-input
          v-model="queryParams.productNo"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料单位" prop="productUnit">
        <el-input
          v-model="queryParams.productUnit"
          placeholder="请输入物料单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="需求数量" prop="totalQty">
        <el-input
          v-model="queryParams.totalQty"
          placeholder="请输入需求数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          :disabled="!(orderData.no&&orderData.status == 1)"-->
<!--        >{{ $t("add") }}</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="!(!single&&orderData.status == 1)"-->
<!--          @click="handleUpdate"-->
<!--        >{{ $t("edit") }}</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="!(!multiple&&orderData.status == 1)"-->
<!--          @click="handleDelete"-->
<!--        >{{ $t("delete") }}</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bill:adjustInDetail:export']"
        >导出</el-button>
      </el-col>

      <el-col :span="1.5">
        <GeneratePDF 
          :header-info="pdfConfig.headerInfo"
          :table-data="adjustInDetailList"
        />
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="adjustInDetailList">
      <el-table-column label="序号" type="index" width="50" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <!-- <el-table-column label="编号" align="center" prop="no" /> -->
      <!-- <el-table-column label="单据编号" align="center" prop="billNo" /> -->
      <af-table-column label="物料编号" align="center" prop="productNo" />
      <af-table-column label="物料名称" align="center" prop="productName" />
      <el-table-column label="需求数量" align="center" prop="qty" />
      <el-table-column label="物料单位" align="center" prop="productUnit" />
      <!-- <el-table-column label="批次号" align="center" prop="batchNo" /> -->
      <!-- <el-table-column label="排序" align="center" prop="sort" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bill:adjustInDetail:edit']"
          >{{ $t("edit") }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bill:adjustInDetail:remove']"
          >{{ $t("delete") }}</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改调拨入库单明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="编号" prop="no">
          <el-input v-model="form.no" placeholder="请输入编号" />
        </el-form-item> -->
        <el-form-item label="单据编号" prop="billNo">
          <el-input v-model="form.billNo" disabled placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="物料编号" prop="productNo">
          <el-select style="width: 100%" v-model="form.productNo" filterable placeholder="请选择物料编号">
            <el-option v-for="dict in productList" :key="dict.productNo" :label="dict.productNo" :value="dict.productNo" @click.native="form.productName = dict.productName; form.productUnit = dict.company"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物料名称" prop="productName">
          <el-input v-model="form.productName" disabled placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料单位" prop="productUnit">
          <el-input v-model="form.productUnit" disabled placeholder="请输入物料单位" />
        </el-form-item>
        <el-form-item label="需求数量" prop="qty">
          <el-input-number v-model="form.qty" placeholder="请输入需求数量" />
        </el-form-item>
        <el-form-item label="批次号" prop="batchNo">
          <el-input v-model="form.batchNo" placeholder="请输入批次号" />
        </el-form-item>
        <!-- <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item> -->
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item> -->
        <!-- <el-form-item label="是否删除：1是 0否" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入是否删除：1是 0否" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t("sure") }}</el-button>
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAdjustInDetail, getAdjustInDetail, delAdjustInDetail, addAdjustInDetail, updateAdjustInDetail, selectAdjustInBillForPrint } from "@/api/invoice/adjustIn";
import { listProduct } from "@/api/basic/product"
import { defaultPageConfig } from "@/const/index";
import GeneratePDF from '@/components/GeneratePDF/index';
import {getNowDate} from '@/utils/index.js'

export default {
  name: "AdjustInDetail",
  inject: ['refresh'],
  props: {
    orderData: {},
  },
  components: {
    GeneratePDF
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 调拨入库单明细表格数据
      adjustInDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 0,
        no: null,
        billNo: null,
        productNo: null,
        productName: null,
        productUnit: null,
        qty: null,
        sort: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productNo: [{ required: true, message: "物料不能为空", trigger: "change" }],
        qty: [
          { required: true, message: "数量不能为空", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value < 1) {
                callback(new Error(that.$t('positiveNumberTip')))
              } else {
                callback()
              }
            },
            trigger: "blur",
          },
        ],
      },
      productList:[],
      pdfConfig: {
        headerInfo: {
          deliveryNo: '2025042100000468',
          businessNo: 'DODL20250421-10',
          sender: '汤振伟',
          senderPhone: '15051848332',
          receiver: '当涂县南商业街店',
          receiverPhone: '赵敏 | 15221006043',
          deliveryAddress: '当涂县兴城路与富民大道交叉口西北40米',
          printTime: '2025-04-21 17:06:04',
          printer: '-1 | 管理员'
        }
      },
    };
  },
  watch: {
    // 监视订单信息变动
    orderData(newValue) {
      if (!newValue || !newValue.no) {
        this.queryParams.billNo = ''
      } else {
        this.queryParams.billNo = newValue.no
      }
      this.getList()
    },
  },
  created() {
    this.getListProduct()
    this.getList();
  },
  methods: {
    /** 获取物料列表 */
    getListProduct() {
      listProduct({ pageNum: 1, pageSize: 0 }).then((response) => {
        this.productList = response.rows
      })
    },
    /** 查询调拨入库单明细列表 */
    getList() {
      this.loading = true;

      if (!this.queryParams.billNo) {
        this.detailList = []
        this.total = 0
        this.loading = false
        return
      }

      let params = {
        ...this.queryParams,
        ...defaultPageConfig
      }

      listAdjustInDetail(params).then(response => {
        this.adjustInDetailList = response.rows;
        this.total = response.total;
        this.loading = false;

        this.getMainInfo()
      });
    },
    getMainInfo(){
      selectAdjustInBillForPrint({ no: this.orderData.no }).then(response => {
        this.pdfConfig.headerInfo = response.data || {}
        this.pdfConfig.headerInfo.printTime = getNowDate()
        this.pdfConfig.headerInfo.billTitle = '调拨入库单'
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        billNo: null,
        productNo: null,
        productName: null,
        productUnit: null,
        qty: null,
        sort: null,
        remark: null,
        delFlag: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.productNos = selection.map((item) => item.productNo)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.billNo = this.orderData&&this.orderData.no?this.orderData.no:''
      this.open = true;
      this.title = "添加调拨入库单明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAdjustInDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改调拨入库单明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAdjustInDetail(this.form).then(response => {
              this.$modal.msgSuccess(this.$t("editSuc"))
              this.open = false;
              this.refresh()
            });
          } else {
            addAdjustInDetail(this.form).then(response => {
              this.$modal.msgSuccess(this.$t("addSuc"))
              this.open = false;
              this.refresh()
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const productNos = row.productNo || this.productNos
      this.$modal.confirm(this.$t('deleteTip', { num: ids.length })).then(function() {
        return delAdjustInDetail(ids);
      }).then(() => {
        this.refresh();
        this.$modal.msgSuccess(this.$t("deleteSuc"))
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bill/adjustInDetail/export', {
        ids: this.ids,
        ...this.queryParams
      }, `调拨入库明细.xlsx`)
    }
  }
};
</script>
